package com.lp.security.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lp.biz.identity.mapper.AdvisorMapper;
import com.lp.biz.identity.mapper.AdvisorMemberApplyMapper;
import com.lp.biz.identity.mapper.AdvisorMemberRelMapper;
import com.lp.biz.identity.model.entity.Advisor;
import com.lp.biz.identity.model.entity.AdvisorMemberApply;
import com.lp.biz.identity.model.entity.AdvisorMemberRel;
import com.lp.biz.member.mapper.MemberMapper;
import com.lp.biz.member.model.entity.Member;
import com.lp.common.component.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.lp.common.constant.AdvisorConstant.CODE_NORMAL_ADVISOR;
import static com.lp.common.constant.CacheConstant.BIZ_USER_DETAIL_KEY;
import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-06-12 23:30:52
 */
@Slf4j
@Component
public class AdvisorSchedule {

    @Autowired
    private RedisService redis;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private AdvisorMapper advisorMapper;

    @Autowired
    private AdvisorMemberRelMapper relMapper;

    @Autowired
    private AdvisorMemberApplyMapper advisorMemberApplyMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    public void autoAgree() {
        log.info("自动审批顾问：开始检查");
        List<AdvisorMemberRel> newList = new ArrayList<>();
        Advisor advisor = advisorMapper.selectOne(new QueryWrapper<Advisor>().lambda().eq(Advisor::getCode, CODE_NORMAL_ADVISOR));
        if (advisor != null) {
            for (AdvisorMemberApply apply : advisorMemberApplyMapper.selectList(new QueryWrapper<AdvisorMemberApply>()
                    .lambda().eq(AdvisorMemberApply::getStatus, NEGATE))) {
                log.info("自动审批顾问：用户【{}】符合条件", apply.getMemberId());
                Long l = relMapper.selectCount(new QueryWrapper<AdvisorMemberRel>().lambda()
                        .eq(AdvisorMemberRel::getMemberId, apply.getMemberId())
                        .eq(AdvisorMemberRel::getIsEffective, SURE));
                AdvisorMemberRel rel = new AdvisorMemberRel();
                rel.setMemberId(apply.getMemberId());
                rel.setAdvisorId(advisor.getId());
                rel.setIsEffective(l == null || l == 0 ? SURE: NEGATE);
                rel.setCreateAt(LocalDateTime.now());
                newList.add(rel);
                Member member = memberMapper.selectById(apply.getMemberId());
                if (member.getRate().compareTo(advisor.getRate()) < 0) {
                    member.setRate(advisor.getRate());
                    memberMapper.updateById(member);
                }
            }
        }
        if (!newList.isEmpty()) {
            relMapper.insertBatch(newList);
            advisorMemberApplyMapper.update(null, new UpdateWrapper<AdvisorMemberApply>().lambda()
                    .in(AdvisorMemberApply::getMemberId, newList.stream()
                            .map(AdvisorMemberRel::getMemberId)
                            .collect(Collectors.toSet()))
                    .set(AdvisorMemberApply::getStatus, SURE));
            for (AdvisorMemberRel rel : newList) {
                redis.delete(BIZ_USER_DETAIL_KEY + rel.getMemberId());
            }
        }
        log.info("自动审批顾问：检查结束");
    }
}
